package com.burgeon.backend.Rest.User;
/**
 * openid搜索会员数据
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.User;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.*;

public class GETUserByOpenid extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GETUserByOpenid(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public User getData(String openid) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(openid), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
           throw new Exception("获取用户错误");
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        JSONArray array = jsonArray.getJSONArray(0);

        User user = new User();
        user.setName(array.getString(0));
        user.setPhone(array.getString(1));
        user.setIntegral(array.getString(2));
        user.setId(array.getString(3));
        user.setType(array.getString(4));
        user.setStore(array.getString(5));
        user.setCustomer(array.getString(6));
        user.setEmployee(array.getString(7));
        user.setBirthday(array.getString(8));
        user.setGender(array.getString(9));
        user.setPrice(array.getString(10));

        return user;
    }

    private MultiValueMap Data(String openid) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("VIPNAME");
        columns.add("CARDNO");
        columns.add("INTEGRAL");//积分
        columns.add("ID");
        columns.add("'C_VIPTYPE_ID;NAME'");//等级
        columns.add("'C_STORE_ID;NAME'");//店仓
        columns.add("OPENCARDDATE");//开卡时间
        columns.add("'HR_EMPLOYEE_ID;NAME'");//开卡人
        columns.add("BIRTHDAY");//生日
        columns.add("SEX");//性别
        columns.add("'FA_VIPACC_ID;AMOUNT'");

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        HashMap paramss = new HashMap();
        String ac = "'="+openid+"'";
        paramss.put("column", "OPENID");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GETUserByOpenid g = new GETUserByOpenid("139.129.93.47:55321","nea@burgeon.com.cn","burgeon55321");
        try {
            System.err.println(g.getData("oFTvdwLkJAppJ-tGXZeCWThz5keg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
